<?php

namespace app\meizhuangapi\model\meizhuang;
use app\meizhuangapi\model\Setting;
use app\storeapi\model\Base;
use think\Db;
use app\storeapi\model\User;
/**
 * 美妆评价模型
 */
class MeizhuangComment extends Base{
    protected $name = 'meizhuang_comment';
    /*
     * 添加评价
     */
    public function addPingjia($data){
        return $this->insertGetId($data);
    }
    /*
     * 获取评价列表
     */
    public function getList($data){
        $list=$this->with(['user'])->where($data)->paginate(15, false, [
            'query' => \request()->request()
        ]);

        $list1=$list->toArray();
        $list=$list1['data'];
        if($list){
            foreach ($list as $k=>$v){
                //获取综合星级
                $zohe=floor(($v['miaoshuStar']+$v['wuliuStar']+$v['fuwuStar'])/3);
                $ctime=strtotime($v['create_time']);
                if($v['imageids']){
                    $imag=explode(',',$v['imageids']);
                    foreach ($imag as $k1=>$v1){
                        $fileInfo=Db::name('upload_file')->where('file_id','=',$v1)->find();
                        $list[$k]['imageInfo'][]=$fileInfo['file_url'].'/'.$fileInfo['file_name'];
                    }
                }else{
                    $list[$k]['imageInfo']=[];
                }
                if((time()-$ctime)>86400&&(time()-$ctime)<86400*15){
                    $list[$k]['timeStr']=floor((time()-$ctime)/86400).'天前';
                }elseif((time()-$ctime)<86400&&(time()-$ctime)>3600){
                    $list[$k]['timeStr']=floor((time()-$ctime)/3600).'小时前';
                }elseif((time()-$ctime)>=86400*15){
                    $list[$k]['timeStr']=floor((time()-$ctime)/86400).'天前';
                }elseif ((time()-$ctime)<=60){
                    $list[$k]['timeStr']='刚刚';
                }elseif ((time()-$ctime)<3600&&(time()-$ctime)>60){
                    $list[$k]['timeStr']=floor((time()-$ctime)/60).'分钟前';
                }
                $list[$k]['zonheStar']=$zohe;
            }
        }
        $list1['data']=$list;
        return $list1;
    }
    /*
     * 关联用户信息
     */
    public function user(){
        return $this->belongsTo('app\storeapi\model\User');
    }

    /*
     * 获取一条评价信息
     */
    public function getOne($data){
        return $this->where($data)->find();
    }
    /*
     * 获取评价总数
     */
    public function getCount($data){
        return $this->where($data)->count();
    }
}
